//
//  UIViewController+ASK.m
//  换换圈
//
//  Created by 艾柯斯 on 14-11-5.
//  Copyright (c) 2014年 艾柯斯. All rights reserved.
//

#import "UIViewController+ASK.h"
//#import "ASKNotifyController.h"

@implementation UIViewController (ASK)

-(IBAction)back {
    [self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

-(void)setNotifyController {
    if (![YMSMember loginMember]) {
        DFAlertView*alter=[[DFAlertView alloc]initWithTitle:@"抱歉" message:@"请先登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter showWithClickedButton:^(NSInteger buttonIndex) {
            UIViewController* controller = [[UIStoryboard storyboardWithName:@"User" bundle:nil] instantiateViewControllerWithIdentifier:@"ASKLoginController"];
            //            [controller setValue:_category forKey:@"type"];
            [self.navigationController pushViewController:controller animated:YES];
        }];

    }
    else
    {
//    UINavigationController* navigationController = self.sideMenuViewController.navigationController;
//    ASKNotifyController*  notifyController = [[ASKNotifyController alloc] init];
//    [navigationController pushViewController:notifyController animated:YES];
    }
}


-(void)setNotifyBarItem {
//    if([ASKMember loginMember]) {
        UIBarButtonItem* notifyItem = [[UIBarButtonItem alloc] initWithIcon:@"ic_notify" highlightedIcon:@"ic_notify_highlighted" target:self action:@selector(setNotifyController)];
        if([UIApplication sharedApplication].applicationIconBadgeNumber) {
            UILabel* label = [[UILabel alloc] init];
            label.font = [UIFont boldSystemFontOfSize:9];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor whiteColor];
            label.backgroundColor  =  [UIColor colorWithRed:0xFF/255.0 green:0x52/255.0 blue:0x52/255.0 alpha:1];
            
            label.layer.masksToBounds = NO;
            label.clipsToBounds = YES;
            label.text = [NSString stringWithFormat:@"%@",@([UIApplication sharedApplication].applicationIconBadgeNumber>99?99:[UIApplication sharedApplication].applicationIconBadgeNumber)];
            CGSize labelSize = [label.text sizeWithFont:label.font];
            UIButton* button = (UIButton*)notifyItem.customView;
            
            label.frame = CGRectMake(button.frame.size.width - labelSize.width - 1, 0 - (labelSize.height/2) + 2, labelSize.width + 6, labelSize.height + 2);
            label.layer.cornerRadius = label.frame.size.height / 2;
            [button addSubview:label];
        }
        self.navigationItem.rightBarButtonItem = notifyItem;
//    }else {
//        self.navigationItem.rightBarButtonItem = nil;
//    }

    
    
    
}


- (void)endEditing {
    [self.view endEditing:YES];
}

- (void)controllerSetting
{
    self.automaticallyAdjustsScrollViewInsets = NO;

    self.navigationController.navigationBar.translucent = YES;
     self.navigationController.navigationBar.barTintColor = MainColor;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark 处理键盘
- (void)keyboardWillShow:(NSNotification *)notification {
    
    NSDictionary *userInfo = [notification userInfo];
    
    
    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardFrame = [self.view convertRect:keyboardFrame fromView:nil];
    // 键盘的动画时间，设定与其完全保持一致
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    
    // 键盘的动画是变速的，设定与其完全保持一致
    NSValue *animationCurveObject = [userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];
    NSUInteger animationCurve;
    [animationCurveObject getValue:&animationCurve];
    
    // 开始及执行动画
    [UIView animateWithDuration:animationDuration delay:0 options:animationCurve<<16 animations:^{
        
    } completion:^(BOOL finished) {;
    }];
    
    
}

-(void)keyboardWillHide:(NSNotification *)notification {
    
    NSDictionary *userInfo = [notification userInfo];
    
    // 键盘的动画时间，设定与其完全保持一致
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    
    // 键盘的动画是变速的，设定与其完全保持一致
    NSValue *animationCurveObject = [userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];
    NSUInteger animationCurve;
    [animationCurveObject getValue:&animationCurve];
    
    
    // 开始及执行动画
    [UIView animateWithDuration:animationDuration delay:0 options:animationCurve<<16 animations:^{
        
    } completion:^(BOOL finished) {
        
    }];
    
}
-(void)viewWillDisappear:(BOOL)animated {
    // 反注册键盘
   
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

@end
